<?php
 mb_internal_encoding("utf-8");
 require "../document/class_corpus.php";
 require "../document/class_document.php";
 require "../document/class_document_xml.php";

 require "../files/tool_files_dirs.php";

 require "../misc/class_tag.php";
 require "../misc/tool_color.php";

 require "../tree/class_tree.php";
 require "../tree/class_tree_xhtml.php";
 require "../tree/class_structure.php";
 require "../tree/tool_tree.php";

 require "../array/tool_array.php";

 require "../pear_graph/tool_pear_graph.php";

 require "../maths/tool_maths.php";

 require "../string/tool_string.php";
 require "../string/class_segment.php";
 require "./class_sqlite_document_new.php";

$dir="../xml/europa_complete_repair/";
$lg_candidates=array("cs","da","de","el","en","es","et","fi","fr","hu","it","lt","lv","nl","pl","pt","sl","sw");
$array_ids_europa=tool_corpus::get_unified_europa($dir,$lg_candidates);
//$motif_lg="fr";
//$motif_lg=implode("|",$lg_candidates);

//$array_ids_europa=tool_corpus::europa_get_all_ids($dir);
$array_corpus_groupe=array();
$start=microtime(true);
//$cpt=0;
//$taille=0;
//$array_bloc_str=array();
//foreach($lg_candidates as $lg){
//if($lg="fr"){
foreach($lg_candidates as $lg){
  $factory_mot=new factory_mot();
  $exp=new sqlite_experience("./sqlite_document.$lg.sdb");
  $array_obj_sql=$exp->reader_experience();

  $all_bloc=$array_obj_sql["bloc"]->reader_all2array();
  foreach($all_bloc as $bloc){
    $factory_mot->get_all_mots($bloc["contenu"]);
  }

  $dictionnaire=$factory_mot->__get_dictionnaire();
foreach($dictionnaire as $graphie=>$array_doc){
 $request_graphie=array("graphie"=>$graphie);
 $id_graphie=$array_obj_sql["graphie"]->writer_values($request_graphie); 
 $array_request=array();

  foreach($array_doc as $id_doc=>$array_bloc){
    foreach($array_bloc as $id_bloc=>$array_segment){
      foreach($array_segment as $id_segment){
        $mot=$factory_mot->__get_word($id_segment);
        $array_request[]=array(
          "id_bloc"=>$id_bloc+1,
          "id_graphie"=>$id_graphie,
          "offset"=>$mot->__get_offset(),//["offset"],
          "len"=>$mot->__get_len()//["len"]
        );
      }
    }
  }

  $array_obj_sql["rstr"]->writer_array_values($array_request);
}
}
$end=microtime(true)-$start;
echo "\n total :: $end $cpt\n";

die();










$array_obj_sql["graphie"];
die();
//print_r($array_obj_sql);
//die();
$all_bloc=$array_obj_sql["bloc"]->reader_all2array();
foreach($all_bloc as $bloc){
  $factory_mot->get_all_mots($bloc["contenu"]);
}

$array_obj_sql_out=array(
  "document"=>new sqlite_document(),
  "bloc"=>new sqlite_bloc(),
  "rstr"=>new sqlite_rstr(),
  "graphie"=>new sqlite_graphie(),
);

$expout=new sqlite_experience("./sqlite_document_word.$lg.sdb");
$expout->writer_create_experience($array_obj_sql_out);

  $connexion_pdo=new PDO("sqlite:./sqlite_document_word.$lg.sdb");
  $connexion_pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  $requete='
    CREATE INDEX indexRstr
    on Rstr (id_bloc,id_graphie)
  ;';
  $prepare_execute = $connexion_pdo->prepare($requete,array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));
  $prepare_execute->execute();
  $connexion_pdo = null;








foreach($array_ids_europa as $id_europa){
  $corpus=tool_corpus::get_corpus_regexp($dir,$id_europa."($motif_lg)");
  $array_doc=$corpus->__get_array_doc();
  foreach($array_doc as $document){
    $id_doc=mb_strcut($document->__get_id(),-6,2);
    //$id_doc=$document->__get_id();
    $structure=$document->__get_structure();
    $signature=$structure->__get_signature();
    tool_tree::cleanstructure($signature);
    $signature->__recalc_idn();

    $test=new RecursiveIteratorIterator($signature,RecursiveIteratorIterator::SELF_FIRST);

    foreach($test as $id_bloc=>$bloc_xhtml){
      $array_bloc_str[] = tool_tree::tree_xhtml2texte($bloc_xhtml->__get_value());
      $factory_mot->get_all_mots($array_bloc_str[count($array_bloc_str)-1],$id_doc);
    }
  }
}
//$all_seg=$factory_mot->__get_array_segment();

//$factory_mot->init_etiquetage();
//$start=microtime(true);
//$factory_mot->init_chunk();
$end=microtime(true)-$start;
echo "\n total :: $end";
die();


//$repartition_chunk=$factory_mot->__get_repartiton_chunk();
//$repartition_segment=$factory_mot->__get_repartition_segment();
die();


$res_all=$zipf=$res_type=array();
$res_type["p"]=$res_type["v"]=$res_type["i"]=0;
foreach($repartition_segment as $id_doc=>$array_bloc){
  foreach($array_bloc as $id_bloc=>$array_id_segment){
    foreach($array_id_segment as $id_segment){
      $mot=$factory_mot->__get_word($id_segment);
      $len=$mot->__get_len();
      $str=mb_strtolower($mot->__tostring());
      $type=$mot->__get_attribute("type");
      $id_doc=mb_strcut($id_doc,-6,2);
      $zipf[$id_doc][$str]++;
      if($type=="i") $zipf_i[$id_doc][$str]++;
      if($type=="v") $zipf_v[$id_doc][$str]++;
      if($type=="p") $zipf_p[$id_doc][$str]++;
    }
  }
}

function fill_graph(&$graph,$data_x,$data_y){
  fill_graph_aux($graph,$data_x,$data_y);
  return $graph->plot04();
}

function fill_graph_aux(&$graph,$data_x,$data_y){
  $graph->__add_array_x($data_x);
  $graph->__add_array_y($data_y);
}

$tool_graph=new Tool_pear_graph();
$list_ids_doc="";
foreach($zipf as $id_doc => $res_zipf){
  $list_ids_doc.="[$id_doc]";
  $zipf=tool_maths::zipf($res_zipf);
  fill_graph_aux($tool_graph,array_keys($zipf),array_values($zipf));
  $tool_graph->__add_plot_title("zipf : $id_doc");
}

$tool_graph->__add_title("zipf");
$graph=$tool_graph->plot04();
ob_start();
$graph->done();
$graph_svg=ob_get_contents();
Tool_files::file_write("./svg_zipf/zipf".$list_ids_doc.".svg",$graph_svg);
ob_clean();
die();

?>
